% analyImgSetPSNR
%%
clear
scale = 4;
solver_mode = 'gpu 0';

%%
Sets = {'Set5', 'Set14', 'Urban100'}; %, 'B100', 'General100'};
% Sets = {'Set14'};
Models   = {'VDSR0', 'FSRCNN', 'RED', 'DRRN'}; %, 'SRCNN'
flag     = [   1,       0,       0,      0];
Versions = {scale, scale, scale, scale};
ImgIdx = [];
% ImgIdx = 59;

% ImgIdx = {
%     3 3 5
%     11 11 8
%     43 93 44
%     };

% load(fullfile(srpath.getMatPath, 'PSNRSets.mat'));
% fid = fopen(fullfile(srpath.getMatPath, 'log.txt'), 'w');

close all
for mi = 1 : length(Models)
    if flag(mi) == 0, continue; end
    hDeploy = srmodel.getDeployFunc(Models{mi}, Versions{mi});
    % hDeploy2 = srmodel.getDeployFunc('VDSR+', Models{mi});
    
    for si = 1 : length(Sets)
        ImgPairs = srimg.genLHPairs(Sets{si}, scale, ImgIdx);
        % ImgPairs = srimg.genLHPairs(Sets{si}, scale, ImgIdx{si, mi});
        % fprintf(fid, '%s, %s\n', Models{mi}, Sets{si});
        
        hFunc = @(x, y) y;
        if strcmpi(Models{mi}, 'RED')
            hFunc = @(x, y) x + y;
        end
%         if strcmpi(Models{mi}, 'FSRCNN') || strcmpi(Models{mi}, 'VDSR0')
%             ImgPairs = ImgPairs(:, [1, 3]);
%         else
%             ImgPairs = ImgPairs(:, 2 : 3);
%         end
        
        [~, PSNR1, SSIM1] = srmodel.deployPairs(ImgPairs(:, 2:3), scale, solver_mode, hDeploy, hFunc);
        fprintf('%.4f/%.4f\n', mean(PSNR1), mean(SSIM1));
        
        for k = 1 : 2
            ImgPairs(:, 1) = srmodel.deployPairs(srimg.imresize(ImgPairs(:, 1), scale/2, 'bicubic'), [], solver_mode, hDeploy, hFunc);
        end
        PSNR2 = srimg.psnr(ImgPairs(:, [1 end]), scale);
        SSIM2 = srimg.ssim(ImgPairs(:, [1 end]), scale);
        fprintf('%.4f/%.4f\n', mean(PSNR2), mean(SSIM2));
        
        SRResult.(Sets{si}) = [PSNR1 PSNR2 SSIM1 SSIM2];
        
        % [ImgPairs2, PSNR2, SSIM2] = srmodel.deployPairs(ImgPairs1, scale, solver_mode, hDeploy2);
        % fprintf('%.4f/.4f, %.4f/.4f\n', mean(PSNR1), mean(SSIM1), mean(PSNR2), mean(SSIM2));
        
        %         PSNRSets.(Models{mi}).(Sets{si}) = [PSNR1, SSIM1];
        % PSNRs.(Models{mi}).(Sets{si}) = [PSNR1, SSIM1, PSNR2, SSIM2];
        % Imgs.(Models{mi}).(Sets{si}) = {ImgPairs{1}, ImgPairs1{1}, ImgPairs2{1}, ImgPairs{2}};
    end
end
% fclose(fid);
save(fullfile(srpath.getMatPath, 'SRResult.mat'), 'SRResult')
% save(fullfile(srpath.getMatPath, 'SRImgs.mat'), 'PSNRs', 'Imgs')

return

%%
Sets = {'Set5', 'Set14', 'Urban100'};
Models = {'FSRCNN', 'RED', 'DRRN'};
load('E:\Academic\X\!SR\2017-01-15-ResSR\code\srcaffe\Models\PSNRSets.mat')

clc
% for si = 1 : length(Sets)
%     for mi = 1 : length(Models)
%         psnr = PSNRSets.(Models{mi}).(Sets{si});
%         vv = [
%             mean(psnr(:, 1)), psnr(:, 1)'
%             mean(psnr(:, 2)), psnr(:, 2)'
%             ];
%         for i = 1 : 2
%             if i == 1
%                 fprintf('%s\t', Models{mi})
%             else
%                 fprintf('VDSR(%s)\t', Models{mi})
%             end
%             fprintf('%.4f\t', vv(i, :)); fprintf('\n');
%         end
%         [~, k] = max(diff(vv));
%         fprintf('%d\t', k-1);
%         fprintf('%.4f\t', diff(vv)); fprintf('\n');
%     end
%     fprintf('\n')
% end

for si = 1 : length(Sets)
    for mi = 1 : length(Models)
        psnr = PSNRSets.(Models{mi}).(Sets{si});
        vv = [
            mean(psnr(:, 1))
            mean(psnr(:, 2))
            ];
        for i = 1 : 2
            if i == 1
                fprintf('%s\t', Models{mi})
            else
                fprintf('%s+DRLN\t', Models{mi})
            end
            fprintf('%.4f\t', vv(i, :)); fprintf('\n');
        end
    end
    fprintf('\n')
end

